<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\commands;

use app\models\Book;
use app\models\Log;
use yii\console\Controller;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class HelloController extends Controller
{
    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     */
    public function actionIndex($message = 'hello world')
    {
        while(1){
            if($this->vget("http://y.com/default/sooo",'')){
                echo $message . "\n";
            }

        }

    }

    /**
     * 开始任务抓取
     * @author  LiShaoWei
     * @access  public
     * @since   1.0
     */
    public function actionSoo()
    {
        $BookModel= new Book();
        $allTitle=$BookModel::find()->select(['id','title','socure'])->where(['is_state'=>0])->orderBy('create_time desc')->limit('10')->asArray()->all();
        foreach($allTitle as $u=>$url){
            $i=$this->Cai($url['socure'],$url['id']);
            echo $url['title'].'成功解析'. $i.'条';
            $Book1Model= new Log();
            $allTitle1=$Book1Model::find()->select(['id','url'])->where(['book_id'=>$url['id'],'is_state'=>0])->limit('10')->asArray()->all();
            foreach($allTitle1 as $u1=>$url1){
                $i=$this->zcontent($url1['socure'],$url1['id']);
                echo $url1['title'].'成功抓取'. $i.'条';
                $Book1Model::find()->createCommand()->update('log',['is_state'=>1],['id'=>$url['id']])->execute();
            }
            $BookModel::find()->createCommand()->update('book',['is_state'=>1],['id'=>$url['id']])->execute();
        }
    }

    public function actionSooo()
    {

    }
    public function generateuuid()
    {
        // $this->Cai(substr($array1[0],0,-1),$id);
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = '';
        $hyphen = chr(45);
        $uuid = substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12);
        return $uuid;
    }
    public function Cai($url,$id)
    {
        $BookArticleModel= new BookArticle();
        //$url="http://www.biquge.info/22_22712/";
        $strIng=$this->vget($url,'');
//        $tt = '/\[{".*?"}\]/si';
        $strZheng='/<dd>.*?<\/dd>/si';
        preg_match_all ( $strZheng , $strIng , $array );//匹配出地址和内容
        $i=1;
        foreach($array[0] as $s=>$k){
            preg_match('/[0-9]{4,}\w*\.html/si',$k,$arrayUrl); //撇配地址
            if(empty($arrayUrl[0])){
                //return '成功解析'. $i.'条';
                return $i;
            }
            $rt=json_encode($arrayUrl,true);
            $urlTitle=$url.$arrayUrl[0];
            if(!$BookArticleModel::find()->where(['book_id'=>$id,'url'=>$urlTitle])->exists()){
                $BookArticleModel::find()->createCommand()->insert('log',
                    [
                        'log'=>iconv("gbk","UTF-8", $rt) ,
                        'log2'=>iconv("gbk","UTF-8", $arrayUrl[0]),
                        'book_id'=>$id,
                        'url'=>$urlTitle,
                        'is_state'=>0,
                        'create_time'=>time()
                    ])->execute();
                $i++;
            }
        }
        // echo '成功解析'. $i.'条';
        return $i;
    }


    public function zcontent($urlTitle,$id)
    {
        $BookArticleModel= new BookArticle();
        $strIngTitleContent=$this->vget($urlTitle,'');

        preg_match('/<h1>.*?<\/h1>/si',$strIngTitleContent,$arrayTitle); //匹配标题
        $title=strtr($arrayTitle[0],array('<h1>'=>'','</h1>'=>''));//去除多余
        preg_match('/id="content".*?<\/div>/si',$strIngTitleContent,$arrayTitleContent); //匹配标题
        $arrayContent=strtr($arrayTitleContent[0],array('&nbsp;'=>'','</div>'=>''));//去除多余
        $pattern = "/<.*>/";
        $pattern1="/id=.*? >/si";
        $content= preg_replace($pattern,"",$arrayContent);
        $content1= preg_replace($pattern1,"",$content);
        $title=iconv("gbk","UTF-8", $title);
        $i=0;
        if(!$BookArticleModel::find()->where(['book_id'=>$id,'title'=>$title])->exists()){
            $BookArticleModel::find()->createCommand()->insert('book_article', [
                'book_id'=>$id,
                'title'=>iconv("gbk","UTF-8", $title) ,
                'content'=>iconv( "gbk","UTF-8", $content1) ,
                'url'=>$urlTitle,
                'create_time'=>time()
            ])->execute();
            $i++;
        }
        return $i;
    }
    function vget ( $url , $ucookie )
    { // 模拟登录获取Cookie函数

        $curl = curl_init (); // 启动一个CURL会话
        curl_setopt ( $curl , CURLOPT_URL , $url ); // 要访问的地址
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYHOST , 2 ); // 从证书中检查SSL加密算法是否存在
       // curl_setopt ( $curl , CURLOPT_USERAGENT , $_SERVER['HTTP_USER_AGENT'] ); // 模拟用户使用的浏览器
        curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION , 1 ); // 使用自动跳转
        curl_setopt ( $curl , CURLOPT_AUTOREFERER , 1 ); // 自动设置Referer
        curl_setopt ( $curl , CURLOPT_COOKIEJAR , $ucookie ); // 存放Cookie信息的文件名称
        curl_setopt ( $curl , CURLOPT_COOKIEFILE , $ucookie ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl , CURLOPT_TIMEOUT , 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl , CURLOPT_HEADER , 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
}
